package com.admin.service

import com.admin.dao.AccountAdminDao
import com.data.annotation.NoargAnnotation
import com.data.annotation.OpenAnnotation
import com.data.bean.Account
import com.data.entity.IndexAdminState
import com.data.util.CheckDataUtils
import com.data.util.DatabaseIdUtils
import com.data.util.FinalStringUtils
import com.data.util.Md5CodeUtils
import org.apache.log4j.Logger
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
@OpenAnnotation
@NoargAnnotation
class AccountAdminService {

    @Autowired
    private var accountAdminDao: AccountAdminDao? = null

    /**
     * 查询账号列表
     */
    fun accountListService(): List<Account> {
        return accountAdminDao!!.accountListDao()
    }

    fun deleteAccountService(id: String): Boolean {
        if (CheckDataUtils.stringUtilsID(id)) {
            return accountAdminDao!!.deleteAccountDao(id.trim()) > 0
        }
        return false
    }

    fun changeAccountService(id: String, state: Boolean): Boolean {
        if (CheckDataUtils.stringUtilsID(id)) {
            val accountAdmin = IndexAdminState(id = id, state = state, states = !state)
            return accountAdminDao!!.changeAccountDao(accountAdmin) > 0
        }
        return false
    }

    fun addAccountService(account: Account): Boolean {
        if (CheckDataUtils.stringUtils(account.email, account.username, account.phone)) {
            account.id = DatabaseIdUtils.dataIdOne
            //默认使用邮箱作为密码
            account.password = Md5CodeUtils.md5(account.email)
            //判断身份是否选择有误
            account.types = when (account.types) { FinalStringUtils.ADMIN, FinalStringUtils.USER -> account.types
                else -> 2
            }

            return accountAdminDao!!.saveMassageDao(account) > 0
        }
        return false
    }

    fun checkAccountService(username: String, type: String): Boolean {
        println(type)
        println(CheckDataUtils.stringUtils(type, username))
        return if (CheckDataUtils.stringUtils(type, username)) {
            when (type) {
                FinalStringUtils.USERNAME -> accountAdminDao!!.checkAccountDao(username, 1).isEmpty()
                FinalStringUtils.EMAIL -> accountAdminDao!!.checkAccountDao(username, 2).isEmpty()
                FinalStringUtils.PHONE -> accountAdminDao!!.checkAccountDao(username, 3).isEmpty()
                else -> false
            }
        } else false

    }
}
